package com.xja.ssm.controller;

import com.xja.ssm.entity.House;
import com.xja.ssm.entity.Users;
import com.xja.ssm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("guanli")
    public String guanli(Model model){
        try {
            List<House> houseList = adminService.selectHouseList();
            model.addAttribute("houseList",houseList);
            return "/page/guanli";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

    @RequestMapping("fabu")
    public String fabu(House house, HttpSession session){
        try {
            Users loginuser = (Users) session.getAttribute("loginuser");
            if (loginuser==null){
                return "redirect:/page/login.jsp";
            }
            house.setUserId(loginuser.getId());
            adminService.addHouse(house);
            return "redirect:/admin/guanli.do";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/error.jsp";
    }

}
